package com.softlab.retailwholesalesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softlab.retailwholesalesystem.entity.OutInWarehouseEntity;
import com.softlab.retailwholesalesystem.entity.VO.OutInWarehouseGoodsVOEntity;
import com.softlab.retailwholesalesystem.entity.StoreEntity;
import com.softlab.retailwholesalesystem.mapper.OutInWarehouseMapper;
import com.softlab.retailwholesalesystem.mapper.StoreMapper;
import com.softlab.retailwholesalesystem.service.IDetailOutWarehouseService;
import com.softlab.retailwholesalesystem.service.IOutInWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OutInWarehouseServiceImpl extends ServiceImpl<OutInWarehouseMapper, OutInWarehouseEntity>  implements IOutInWarehouseService {

    @Autowired
    OutInWarehouseMapper mapper;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private IDetailOutWarehouseService detailOutWarehouseService;


    @Override
    public List<OutInWarehouseEntity> search(OutInWarehouseEntity outInWarehouseEntity) {
        QueryWrapper<OutInWarehouseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (outInWarehouseEntity.getOutInWarehouseId() != null) wrapper.eq("out_in_warehouse_id",outInWarehouseEntity.getOutInWarehouseId());
        if (outInWarehouseEntity.getLastModifiedName() != null) wrapper.eq("last_modified_name",outInWarehouseEntity.getLastModifiedName());
        if (outInWarehouseEntity.getState() != null) wrapper.eq("state",outInWarehouseEntity.getState());
        return mapper.selectList(wrapper);
    }

    @Override
    public Integer confirm(OutInWarehouseEntity entity) {
        Long outInWarehouseId = entity.getOutInWarehouseId();
        List<OutInWarehouseGoodsVOEntity> map = detailOutWarehouseService.getMap(outInWarehouseId);

        for (OutInWarehouseGoodsVOEntity e:
             map) {
            Long outWarehouseId = e.getOutWarehouseId();
            Long inWarehouseId = e.getInWarehouseId();
            Long goodsId = e.getGoodsId();
            Long amount = e.getAmount();

            QueryWrapper<StoreEntity> storeEntityQueryWrapper = new QueryWrapper<>();
            storeEntityQueryWrapper.eq("goods_id", goodsId);
            //storeEntityQueryWrapper.eq("goods_number", goodsNumber);
            storeEntityQueryWrapper.eq("repository_id", outWarehouseId);
            storeEntityQueryWrapper.eq("is_delete", 0);
            List<StoreEntity> storeEntities = storeMapper.selectList(storeEntityQueryWrapper);
            if (storeEntities.isEmpty())
                return 404; //出库仓库没有该商品
            StoreEntity out = storeEntities.get(0);
            if (out.getGoodsNumber() < amount)
                return 500; //出库仓库商品不足

            out.setGoodsNumber(out.getGoodsNumber() - (Integer) amount.intValue());
            storeMapper.updateById(out);
            if (out.getGoodsNumber() == 0){
                out.setIsDelete(1);
                storeMapper.updateById(out);
            }


            storeEntityQueryWrapper = new QueryWrapper<>();
            storeEntityQueryWrapper.eq("goods_id", goodsId);
            //storeEntityQueryWrapper.eq("goods_number", goodsNumber);
            storeEntityQueryWrapper.eq("repository_id", inWarehouseId);
            storeEntityQueryWrapper.eq("is_delete", 0);
            List<StoreEntity> storeEntities1 = storeMapper.selectList(storeEntityQueryWrapper);
            if (storeEntities1.isEmpty()){
                StoreEntity in = new StoreEntity();
                in.setIsDelete(0);
                in.setRepositoryId((Integer) inWarehouseId.intValue());
                in.setGoodsNumber((Integer) amount.intValue());
                in.setGoodsId((Integer) goodsId.intValue());
                storeMapper.insert(in);
            }else {
                StoreEntity in = storeEntities1.get(0);
                in.setGoodsNumber(in.getGoodsNumber() + (Integer) amount.intValue());
                storeMapper.updateById(in);
            }

        }
        return 200;
    }
}
